﻿using System;
using System.Linq;
using System.Web.Security;

public partial class SearchSite : System.Web.UI.Page
{

    public aspnet_User currentUser;
    public DatenightBLL datenightBLL;
    public PersonBLL personBLL;
    public CityBLL cityBLL;

    protected void Page_Load(object sender, EventArgs e)
    {
        datenightBLL = DatenightBLL.Instance;
        personBLL = PersonBLL.Instance;
        cityBLL = CityBLL.Instance;

        if (!IsPostBack)
        {

            currentUser = personBLL.getCurrentUser(Membership.GetUser());
            string path = Server.MapPath("../images/ProfilePictures/");

            LWDatelist.DataSource = datenightBLL.returnSearchList(currentUser, path);
            LWDatelist.DataBind();

            selectPlace.DataSource = datenightBLL.getAllRegions();
            selectPlace.DataBind();

            selectGender.DataSource = datenightBLL.getAllGenders();
            selectGender.DataBind();
        }
    }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static string[] GetCompletionList(string prefixText, int count, string contextKey)
    {
        return DatenightBLL.Instance.getCitysForCompletionlist(prefixText).ToArray();
    }

    protected void selectGender_SelectedIndexChanged(object sender, EventArgs e)
    {
        string path = Server.MapPath("../images/ProfilePictures/");
        string gender = selectGender.SelectedItem.Value;
        var currentUser = personBLL.getCurrentUser(Membership.GetUser());
        if (gender == "0")
        {
            LWDatelist.DataSource = datenightBLL.returnSearchList(currentUser, path);
            LWDatelist.DataBind();
            selectPlace.Enabled = false;
            CityRadiusDDL.Enabled = false;
            CityZipcodeTxt.Enabled = false;
            selectPlace.SelectedIndex = 0;
        }

        else
        {
            LWDatelist.DataSource = datenightBLL.returnSearchList(currentUser, gender, path);
            LWDatelist.DataBind();
            selectPlace.Enabled = true;
            CityZipcodeTxt.Enabled = true;
            CityRadiusDDL.Enabled = true;
            selectPlace.SelectedIndex = 0;
        }
        TxtAgeMax.Enabled = false;
        TxtAgeMin.Enabled = false;

    }

    protected void selectPlace_SelectedIndexChanged(object sender, EventArgs e)
    {
        string path = Server.MapPath("../images/ProfilePictures/");
        string place = selectPlace.SelectedItem.Value;
        string gender = selectGender.SelectedItem.Value;
        var currentUser = personBLL.getCurrentUser(Membership.GetUser());

        if (place == "0")
        {
            LWDatelist.DataSource = datenightBLL.returnSearchList(currentUser, gender, path);
            LWDatelist.DataBind();
            TxtAgeMax.Enabled = false;
            TxtAgeMin.Enabled = false;
            TxtAgeMax.Text = "";
            TxtAgeMin.Text = "";
        }
        else
        {
            LWDatelist.DataSource = datenightBLL.returnSearchList(currentUser, gender, place, path);
            LWDatelist.DataBind();
            TxtAgeMax.Enabled = true;
            TxtAgeMin.Enabled = true;
            TxtAgeMax.Text = "";
            TxtAgeMin.Text = "";
        }

    }

    protected void TxtAgeMin_TextChanged(object sender, EventArgs e)
    {
        ageSelectEvent();
    }

    protected void TxtAgeMax_TextChanged(object sender, EventArgs e)
    {
        ageSelectEvent();
    }

    private void ageSelectEvent()
    {
        string path = Server.MapPath("../images/ProfilePictures/");
        string place = selectPlace.SelectedItem.Value;
        string gender = selectGender.SelectedItem.Value;
        string agemin = TxtAgeMin.Text;
        string agemax = TxtAgeMax.Text;
        var currentUser = personBLL.getCurrentUser(Membership.GetUser());

        if (agemin.Length == 0 && agemax.Length == 0)
        {
            LWDatelist.DataSource = datenightBLL.returnSearchList(currentUser, gender, place, path);
            LWDatelist.DataBind();
        }

        else if (agemin.Length == 2 && agemax.Length == 0)
        {
            LWDatelist.DataSource = datenightBLL.returnSearchList(currentUser, place, gender, Convert.ToInt32(agemin), path);
            LWDatelist.DataBind();
        }

        else if (agemin.Length == 0 && agemax.Length == 2)
        {
            LWDatelist.DataSource = datenightBLL.returnSearchList(currentUser, Convert.ToInt32(agemax), gender, place, path);
            LWDatelist.DataBind();
        }

        else if (agemin.Length == 2 && agemax.Length == 2)
        {
            LWDatelist.DataSource = datenightBLL.returnSearchList(currentUser, gender, place, Convert.ToInt32(agemin), Convert.ToInt32(agemax), path);
            LWDatelist.DataBind();
        }
    }


    protected void DistanceSliderShowBox_TextChanged(object sender, EventArgs e)
    {
    }

    protected void CityRadiusDDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        string cityname = CityZipcodeTxt.Text;
        string zipcode = CityZipcodeTxt.Text;
        if (cityname.Length > 8)
        {
            
            cityname = cityname.Substring(0, cityname.Length - 7);
            
            zipcode = zipcode.Substring(zipcode.Length - 5, 4);
            var CityObj = cityBLL.FindCityByNameAndZipcode(cityname, zipcode);
            var currentUser = personBLL.getCurrentUser(Membership.GetUser());
            string path = Server.MapPath("../images/ProfilePictures/");
            string gender = selectGender.SelectedItem.Value;

            var Citys = cityBLL.FindCitysWithinDistance(CityObj, Convert.ToDouble(CityRadiusDDL.SelectedValue));
            LWDatelist.DataSource = datenightBLL.returnSearchList(currentUser, gender, Citys, path);
            LWDatelist.DataBind();
        }
    }
}